package Bug;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2024/6/19 17:09
 * @description:
 * @version:
 */
public class User {
    private final String firstName; // 必需的属性
    private final String lastName; // 必需的属性
    private final int age; // 可选的属性
    private final String email; // 可选的属性

    private User(UserBuilder builder) {
        this.firstName = builder.firstName;
        this.lastName = builder.lastName;
        this.age = builder.age;
        this.email = builder.email;
    }

    // 公共方法访问属性
    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public int getAge() {
        return age;
    }

    public String getEmail() {
        return email;
    }

    // 静态内部类 Builder
    public static class UserBuilder {
        private final String firstName;
        private final String lastName;
        private int age;
        private String email;

        public  UserBuilder(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public UserBuilder age(int age) {
            this.age = age;
            return this; // 返回Builder对象以支持链式调用
        }

        public UserBuilder email(String email) {
            this.email = email;
            return this; // 返回Builder对象以支持链式调用
        }

        // 构建方法，返回最终的User对象
        public User build() {
            return new User(this);
        }
    }

    // 示例用法
    public static void main(String[] args) {
        User user = new UserBuilder("John", "Doe")
                .age(30)
                .email("john.doe@example.com")
                .build();
        System.out.println("User: " + user.getFirstName() + " " + user.getLastName());
        System.out.println("Age: " + user.getAge());
        System.out.println("Email: " + user.getEmail());
    }
}